/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sergivonavi.materialbanner.app.slice;

import com.sergivonavi.materialbanner.app.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import java.io.IOException;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    @Override
    public void onStart(Intent intent) {
        try {
            int color = getResourceManager().getElement(ResourceTable.Color_title_bar_bg).getColor();
            getWindow().setStatusBarColor(color);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Component about = findComponentById(ResourceTable.Id_btn_about);
        if (about instanceof Text) {
            Button btnAbout = (Button) about;
            btnAbout.setClickedListener(this);
        }
        Component showcase = findComponentById(ResourceTable.Id_btn_showcase);
        if (showcase instanceof Text) {
            Button btnShowcase = (Button) showcase;
            btnShowcase.setClickedListener(this);
        }
        Component layout = findComponentById(ResourceTable.Id_btn_from_layout);
        if (layout instanceof Text) {
            Button btnLayout = (Button) layout;
            btnLayout.setClickedListener(this);
        }
        Component from = findComponentById(ResourceTable.Id_btn_from_Ability);
        if (from instanceof Text) {
            Button btnFrom = (Button) from;
            btnFrom.setClickedListener(this);
        }
        Component styled = findComponentById(ResourceTable.Id_btn_styled);
        if (styled instanceof Text) {
            Button btnStyled = (Button) styled;
            btnStyled.setClickedListener(this);
        }
        Component global = findComponentById(ResourceTable.Id_btn_global_style);
        if (global instanceof Text) {
            Button btnGlobal = (Button) global;
            btnGlobal.setClickedListener(this);
        }
        Component padding = findComponentById(ResourceTable.Id_btn_with_padding);
        if (padding instanceof Text) {
            Button btnPadding = (Button) padding;
            btnPadding.setClickedListener(this);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn_about:
                toAbout();
                break;
            case ResourceTable.Id_btn_showcase:
                toShowCase();
                break;
            case ResourceTable.Id_btn_from_layout:
                toFromLayout();
                break;
            case ResourceTable.Id_btn_from_Ability:
                toFromAbility();
                break;
            case ResourceTable.Id_btn_styled:
                toStyled();
                break;
            case ResourceTable.Id_btn_global_style:
                toGlobalStyle();
                break;
            case ResourceTable.Id_btn_with_padding:
                toWithPadding();
                break;
        }
    }

    private void toWithPadding() {
        Intent intent7 = new Intent();
        Operation operation7 = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.sergivonavi.materialbanner.app")
                .withAbilityName("com.sergivonavi.materialbanner.app.WithPaddingAbility")
                .build();
        intent7.setOperation(operation7);
        startAbility(intent7);
    }

    private void toGlobalStyle() {
        Intent intent6 = new Intent();
        Operation operation6 = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.sergivonavi.materialbanner.app")
                .withAbilityName("com.sergivonavi.materialbanner.app.GlobalStyleAbility")
                .build();
        intent6.setOperation(operation6);
        startAbility(intent6);
    }

    private void toStyled() {
        Intent intent5 = new Intent();
        Operation operation5 = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.sergivonavi.materialbanner.app")
                .withAbilityName("com.sergivonavi.materialbanner.app.StyledBannerAbility")
                .build();
        intent5.setOperation(operation5);
        startAbility(intent5);
    }

    private void toFromAbility() {
        Intent intent4 = new Intent();
        Operation operation4 = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.sergivonavi.materialbanner.app")
                .withAbilityName("com.sergivonavi.materialbanner.app.FromCodeAbility")
                .build();
        intent4.setOperation(operation4);
        startAbility(intent4);
    }

    private void toFromLayout() {
        Intent intent3 = new Intent();
        Operation operation3 = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.sergivonavi.materialbanner.app")
                .withAbilityName("com.sergivonavi.materialbanner.app.FromLayoutAbility")
                .build();
        intent3.setOperation(operation3);
        startAbility(intent3);
    }

    private void toShowCase() {
        Intent intent2 = new Intent();
        Operation operation2 = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.sergivonavi.materialbanner.app")
                .withAbilityName("com.sergivonavi.materialbanner.app.ShowcaseAbility")
                .build();
        intent2.setOperation(operation2);
        startAbility(intent2);
    }

    private void toAbout() {
        Intent intent1 = new Intent();
        Operation operation1 = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.sergivonavi.materialbanner.app")
                .withAbilityName("com.sergivonavi.materialbanner.app.AboutAbility")
                .build();
        intent1.setOperation(operation1);
        startAbility(intent1);
    }
}
